package main;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;

public class Client extends Thread {
	
	
	ArrayList<Request> requestList;
	Catalog catalog;
	
	public Client(String requestFile,Catalog c)
	{
		requestList = new ArrayList<Request>();
		readClientFile(requestFile);
		catalog = c;
		
	}
	
	
	private void readClientFile(String file)
	{
	
			try {
				
				Scanner scan= new Scanner(new FileInputStream(file));
							
				while(scan.hasNext())
				{
					Scanner lineScan = new Scanner(scan.nextLine());
					lineScan.useDelimiter("#");
					String firstName=lineScan.next();
					String lastName =lineScan.next();
					String eventName =lineScan.next();
					int hallId =lineScan.nextInt();
					String date =lineScan.next();
					String time =lineScan.next();
					int seats = lineScan.nextInt();
								
					
					requestList.add(new Request(firstName, lastName, eventName, hallId, date, time, seats));
					
					
				}
				
				
				
				
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			}
		
	}
	
	public String toString()
	{
		String c_record="";
		
		for(Request e : requestList)
		{
			c_record += e + "\n----------------------------------- \n";
		}
		
		return c_record;
		
	}

	//public Event getEvent(String event, int hall, String date, String time)
	public void run()
	{
		for(Request r : requestList)
		{
			Event event=catalog.getEvent(r.r_eventName,r.r_hallId,r.r_date,r.r_time);
			if(event==null)
			{
				System.out.println("Error   : Request ->" + r.toString() + "not matched");
			}
			else
			{
				event.book(r);
			}
		}
		
		//System.out.println("Run Completed!");
		
	}
	
}
